$(function(){
	 lay('#version').html('-v'+ laydate.v);
	    //时间选择器
	    laydate.render({
	      elem: '#startTime',
	      min: minDate(),
	      btns: ['clear', 'confirm']
	      
	    });
	    laydate.render({
	        elem: '#endTime',
	        min: minDate(),
		    btns: ['clear', 'confirm']	  
	    });
	   
})
function minDate(){
    var now = new Date();
    return now.getFullYear()+"-" + (now.getMonth()+1) + "-" + now.getDate();
 }

layui.use('table',function(){
  var table = layui.table;
  var form = layui.form;
 
 
 
  
 //监听表格复选框选择
  table.on('checkbox(demo)', function(obj){
	  
       form.render(); //单选复选下拉框渲染
	  var data=obj.data;
	 if(obj.type=="all"){
		
		 
		 $("#checkedDiv").html("")
		 var tables=table.cache.idTest;
		 if(obj.checked==true){
			 
			 	if($("#musicalText").val()==""){
			 		 $.each(tables,function(i,item){
								 $("#musicalText").val($("#musicalText").val()+item.muId+","+item.muName+","+0+"#")
			 		});
			 		addAllProductionMusical();
			 		showDiv();
				}else{
					$.each(tables,function(i,item){
						var count=0;
 
						 var musicalTexts = new Array()
						 var musicalTexts=$("#musicalText").val().split("#");
						 for (var j = 0; j < musicalTexts.length-1; j++) {
							if(musicalTexts[j].split(",")[0]==item.muId){
								 count=1;
							} 
						}
						if(count==0){
							 $("#musicalText").val($("#musicalText").val()+item.muId+","+item.muName+","+0+"#")
						}
					});
			 
					addAllProductionMusical();
					showDiv();
				}
				
 

			  
		 }else{
			   $.each(tables,function(i,item){
				   deleteAllProductionMusical(item)
					
			 })
			 showDiv();
		 }
		 
		 
	 }else{
		 if(obj.checked==true){
			 addBillOfMaterial(data);
			 $("#checkedDiv").append("<div id='checkedDiv"+data.muId+"' class='checkedDiv flag note note--success new-item' onclick='removeChecked(\"checkedDiv"+data.muId+"\",\""+data.muId+"\")'>" +
	  					"<div class='flag__image note__icon'><i class='fa fa-check'></i></div>"+
	  					" <div class='flag__body note__text'>"+data.muName+"</div>"+
						"</div>")
		 }else{
			 deleteBillOfMaterial(data);
			 $("#checkedDiv"+data.muId).removeClass("new-item").addClass("remove-item");
			 setTimeout(() => {
				 $("#checkedDiv"+data.muId).remove();
			}, 500);
		 }
	 }
 
  });
 
 
  
  //表单重载
  var $ = layui.$, active = {
		    reload: function(){
		      var muName = $('#muName');
		      var mtId = $('#mtId');
		      var startPrice = $('#startPrice');
		      var endPrice = $('#endPrice');
		      //执行重载    查询
		      table.reload('idTest', {
		        page: {
		          curr: 1 //重新从第 1 页开始
		        }
		        ,where: {
		        	key: {
		        		title: muName.val(),
		        		titletype: mtId.val(),
			        	titlestartPrice: startPrice.val(),
			        	titleEndPrice: endPrice.val(),
		        	}
		        }
		      });
		    }
		  };
  
  $('#musicalSreach').on('click', function(){
    var type = $(this).data('type');
    active[type] ? active[type].call(this) : '';
  });
  
 

  
 
});
layui.use(['form', 'table'], function(){
	  var form = layui.form
	  var table = layui.table
	  var layer = layui.layer
	  
	var url="../../listenMusicalType/selectAll.action";
  	var data=null
  	$.post(url,data,function(mes){
  		 $.each(mes.data,function(i,item){
  				 $("#mtId").append("<option value="+item.mtId+">"+item.mtName+"</option>")
    				  load();
		  })
  	},"json");
  	
  	
  	form.on('submit(formEdit)', function(){
	if(!$("#startTime").val()){
		layer.msg("请选择开始时间")
		 return ;
	}
	
	if(!$("#endTime").val()){
		layer.msg("请选择结束时间")
		 return ;
	}
	  if(!$("#musicalText").val()){
		  layer.msg("请添加乐器")
		  return ;
	  }else{
		  
		 
  			var booleans=false;
	  			 var musicalTexts = new Array()
				 var musicalTexts=$("#musicalText").val().split("#");
			    for (var i = 0; i < musicalTexts.length; i++) {
			    	if(musicalTexts[i]){
			    		var number=$("#number_"+musicalTexts[i].split(",")[0]).val()
						if(parseInt(number)<1){
							booleans=false;
							break ;
						}else{
							booleans=true;
						}
			    	}
			    	
				}
			    console.info(boolean)
			    if(booleans){
			    	  var boolean=true;
					  var alert="";
						
						 $.ajaxSetup({
								async: false
							});
								 var url="../../listenBillOfMaterial/inspectStock.action";
								  	var data={"muId":$("#musicalText").val()}
								  	$.post(url,data,function(mes){
								  		 $.each(mes,function(i,item){
								  			 if(parseInt(item.productionNumber)>parseInt(item.rawstockNumber)){
								  				boolean=false;
								  				alert=alert+item.muName+","+item.rawstockName+","+item.productionNumber+","+item.rawstockNumber+"*";
								  			 }
								  		 })
								  	},"json");
					  	$.ajaxSetup({
							async: true
						});
					  	
					  	if(boolean==false){
					  		 
							layer.open({
					    		 title: "材料不足提示",
					    		  type: 2, //设置为iframe
					    		  area: ['100%', '400px'],
					    		  content: ['lowStocks.html?dataText='+alert] //这里content是一个URL，如果你不想让iframe出现滚动条，你还可以content: ['http://sentsin.com', 'no']
					    	  }); 
					}else{
						  var production=$("#production").val()+","+$("#startTime").val()+","+$("#endTime").val();
						  var url="../../listenProduction/addOrUpdate.action";
						  	var data={"production":production,"musicalText":$("#musicalText").val(),"addOrUpdate":$("#addOrUpdate").val()}
						  	$.post(url,data,function(mes){
						  		if(mes>0){
									  window.parent.location.reload()
									  var index=parent.layer.getFrameIndex(window.name);
									  parent.layer.close(index);
								  }
						  	},"json");
					}
			    }else{
			    	layer.msg("请输入制造乐器数量,不能小于0")
			    }
	
	  }

  })

  	  
});
function load(){
	var form = layui.form;
	form.render();
}

function showDiv(){
	 var musicalTexts = new Array()
	 var musicalTexts=$("#musicalText").val().split("#");

	 for (var i = 0; i < musicalTexts.length-1; i++) {
	        $("#checkedDiv").append("<div id='checkedDiv"+musicalTexts[i].split(",")[0]+"' class='flag note note--success new-item' onclick='removeChecked(\"checkedDiv"+musicalTexts[i].split(",")[0]+"\",\""+musicalTexts[i].split(",")[0]+"\")'>"+
								"<div class='flag__image note__icon'><i class='fa fa-check'></i></div>"+
								" <div class='flag__body note__text'>"+musicalTexts[i].split(",")[1]+"</div>"+
									"</div>")
	}
	 
	
}

 

function addAllProductionMusical(){
	$("#productionMusicalTbody").html("")
	 var musicalTexts = new Array()
	 var musicalTexts=$("#musicalText").val().split("#");
	 for (var i = 0; i < musicalTexts.length-1; i++) {
		 $("#productionMusicalTbody").append("<tr>"+
					"<td style='display:none;'>"+musicalTexts[i].split(",")[0]+"</td>" +
					"<td>"+musicalTexts[i].split(",")[1]+"</td>" +
					"<td><input type='text' name='title' id='number_"+musicalTexts[i].split(",")[0]+"' required lay-verify='required' value='"+musicalTexts[i].split(",")[2]+"' autocomplete='off' class='layui-input' style='border:0px;' onkeyup='input(this,event)'></td>" +
					"<td style='text-align:center;'><a class='layui-btn layui-btn-danger layui-btn-xs' lay-event='del' onclick='removeChecked(\"checkedDiv"+musicalTexts[i].split(",")[0]+"\",\""+musicalTexts[i].split(",")[0]+"\")'>删除</a></td>" +
					"</tr>")
				load();
	}
}

function deleteAllProductionMusical(item){
	 var musicalTexts = new Array()
	 var musicalTexts=$("#musicalText").val().split("#");
	 $("#musicalText").val("");
	 for (var i = 0; i < musicalTexts.length-1; i++) {
		if(musicalTexts[i].indexOf(item.muId)!=0){
			 $("#musicalText").val( $("#musicalText").val()+musicalTexts[i]+"#");
		} 
	}
	 
	 
	
	 $("#productionMusicalTbody").html("")
	  musicalTexts=$("#musicalText").val().split("#");
	  for (var i = 0; i < musicalTexts.length; i++) {
		  if(musicalTexts[i]){
			  $("#productionMusicalTbody").append("<tr>"+
						"<td style='display:none;'>"+musicalTexts[i].split(",")[0]+"</td>" +
						"<td>"+musicalTexts[i].split(",")[1]+"</td>" +
						"<td><input type='text' name='title' id='number_"+musicalTexts[i].split(",")[0]+"' required lay-verify='required' value='"+musicalTexts[i].split(",")[2]+"' autocomplete='off' class='layui-input' style='border:0px;height:22px' onkeyup='input(this,event)'></td>" +
						"<td style='text-align:center;'><a class='layui-btn layui-btn-danger layui-btn-xs' lay-event='del' onclick='removeChecked(\"checkedDiv"+musicalTexts[i].split(",")[0]+"\",\""+musicalTexts[i].split(",")[0]+"\")'>删除</a></td>" +
						"</tr>")
					load();
		  }
	  }
}

function addBillOfMaterial(data){
	
	 var musicalTexts = new Array()
	 $("#musicalText").val($("#musicalText").val()+data.muId+","+data.muName+","+0+"#")
	 musicalTexts=$("#musicalText").val().split("#");
	 $("#productionMusicalTbody").html("")
	  for (var i = 0; i < musicalTexts.length; i++) {
		  if(musicalTexts[i]){
			  $("#productionMusicalTbody").append("<tr>"+
						"<td style='display:none;'>"+musicalTexts[i].split(",")[0]+"</td>" +
						"<td>"+musicalTexts[i].split(",")[1]+"</td>" +
						"<td><input type='text' name='title' id='number_"+musicalTexts[i].split(",")[0]+"' required lay-verify='required' value='"+musicalTexts[i].split(",")[2]+"' autocomplete='off' class='layui-input' style='border:0px;height:22px' onkeyup='input(this,event)'></td>" +
						"<td style='text-align:center;'><a class='layui-btn layui-btn-danger layui-btn-xs' lay-event='del' onclick='removeChecked(\"checkedDiv"+musicalTexts[i].split(",")[0]+"\",\""+musicalTexts[i].split(",")[0]+"\")'>删除</a></td>" +
						"</tr>")
					load();
		  }
		  
	  }
}

function deleteBillOfMaterial(data){
	var musicalTexts = new Array()
	 var musicalTexts=$("#musicalText").val().split("#");
	 $("#musicalText").val("");
	 for (var i = 0; i < musicalTexts.length-1; i++) {
		if(musicalTexts[i].indexOf(data.muId)!=0){
			 $("#musicalText").val( $("#musicalText").val()+musicalTexts[i]+"#");
		} 
	}
	 
	 
	
	 $("#productionMusicalTbody").html("")
	  musicalTexts=$("#musicalText").val().split("#");
	  for (var i = 0; i < musicalTexts.length; i++) {
		  if(musicalTexts[i]){
			  $("#productionMusicalTbody").append("<tr>"+
						"<td style='display:none;'>"+musicalTexts[i].split(",")[0]+"</td>" +
						"<td>"+musicalTexts[i].split(",")[1]+"</td>" +
						"<td><input type='text' name='title' id='number_"+musicalTexts[i].split(",")[0]+"' required lay-verify='required' value='"+musicalTexts[i].split(",")[2]+"' autocomplete='off' class='layui-input' style='border:0px;height:22px' onkeyup='input(this,event)'></td>" +
						"<td style='text-align:center;'><a class='layui-btn layui-btn-danger layui-btn-xs' lay-event='del' onclick='removeChecked(\"checkedDiv"+musicalTexts[i].split(",")[0]+"\",\""+musicalTexts[i].split(",")[0]+"\")'>删除</a></td>" +
						"</tr>")
					load();
		  }
	  }
}

function removeChecked(id,muId){
	$("#"+id).removeClass("new-item").addClass("remove-item");
	setTimeout(() => {
		$("#"+id).remove();
	}, 500);
	var musicalTexts = new Array()
	 var musicalTexts=$("#musicalText").val().split("#");
	 $("#musicalText").val("");
	 for (var i = 0; i < musicalTexts.length-1; i++) {
		if(musicalTexts[i].indexOf(muId)!=0){
			$("#musicalText").val( $("#musicalText").val()+musicalTexts[i]+"#");
		} 
	}
	
	$('#musical').next().find('.layui-table-body tr').each(function(index,item){
		var ids=$(item).find('td[data-field="muId"]').text();
		if(muId==ids){
			var index=$(item).data('index');
            var che = $('#musical').next().find('.layui-table-body tr[data-index="'+index+'"] input[name="layTableCheckbox"]')
            che.prop("checked",false)
           load();
		}
	});
 
	var musicalTexts = new Array()
	 var musicalTexts=$("#musicalText").val().split("#");
	 $("#productionMusicalTbody").html("")
	  musicalTexts=$("#musicalText").val().split("#");
	  for (var i = 0; i < musicalTexts.length; i++) {
		  if(musicalTexts[i]){
			  $("#productionMusicalTbody").append("<tr>"+
						"<td style='display:none;'>"+musicalTexts[i].split(",")[0]+"</td>" +
						"<td>"+musicalTexts[i].split(",")[1]+"</td>" +
						"<td><input type='text' name='title' id='number_"+musicalTexts[i].split(",")[0]+"' required lay-verify='required' value='"+musicalTexts[i].split(",")[2]+"' autocomplete='off' class='layui-input' style='border:0px;height:22px' onkeyup='input(this,event)'></td>" +
						"<td style='text-align:center;'><a class='layui-btn layui-btn-danger layui-btn-xs' lay-event='del' onclick='removeChecked(\"checkedDiv"+musicalTexts[i].split(",")[0]+"\",\""+musicalTexts[i].split(",")[0]+"\")'>删除</a></td>" +
						"</tr>")
					load();
		  }
	  }
}


function input(obj,event){
	//var che=$(obj).parent().parent().parent().find(".layui-table-col-special input[name='layTableCheckbox']")
	
	
	var keycode = String.fromCharCode(event.which); 
	var first=$(obj).val().substr(0,1);
	var noFs=/^[0-9]*$/;
	 
	var numbers=0;
	var prices=0;
		if($(obj).val()==""){
			$(obj).val(0)
		}else{
			if(!noFs.test($(obj).val())){
				$(obj).val(0)
			}else{
				if(first==0){
					$(obj).val("")
					$(obj).val(keycode)
					
				}
				 
			}
		}
		 
		$("#musicalText").val("")
		$("#productionMusicalTbody").find("tr").each(function(i,item){
			var muId=$(item).children().get(0);
			var muName=$(item).children().get(1);
			 console.info(muName)
			$("#musicalText").val($("#musicalText").val()+$(muId).text()+","+$(muName).text()+","+$("#number_"+$(muId).text()).val()+"#");
		 
		});
		
		 
}


